//
//  LKGXDropDownListButton.m
//  LKFXPT
//
//  Created by Ssky on 2016/11/1.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "LKGXDropDownListButton.h"
#import "UIView+GXAddition.h"

@interface LKGXDropDownListButton ()


@property(nonatomic,weak) UIView * bgBtnView;
@property(nonatomic,weak) UIControl * vCover;

@property (nonatomic, strong) NSArray<UIButton *> * buttons;

@property (nonatomic,weak) UILabel * lblTitle;

@end

@implementation LKGXDropDownListButton
@synthesize text = _text;

///实例化
+ (instancetype)gx_dropDownListWithTextList:(NSArray<NSString *> *)textList dictionary:(NSDictionary *)dict {
    NSString * titleStr = @"--请选择";
    CGFloat fontSizeF = 12;
    UIColor * normalColorCo = [UIColor blackColor];
    if (!textList || textList.count != 0) {
        titleStr = textList[0];
    }
    
    if (fontSizeF > 0) {
        fontSizeF = [dict[@"fontSize"] floatValue];
    }
    
    if (normalColorCo) {
        normalColorCo = dict[@"normalColor"];
    }
    
    LKGXDropDownListButton * btnDDL = [[self alloc] init];
    [btnDDL setupUI];
    
    btnDDL.lblTitle.text = titleStr;
    btnDDL.lblTitle.textColor = normalColorCo;
    btnDDL.lblTitle.font = [UIFont systemFontOfSize:fontSizeF];
    
    [btnDDL.lblTitle setTextAlignment:NSTextAlignmentLeft];
    [btnDDL.lblTitle sizeToFit];
    
    [btnDDL addTarget:btnDDL action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    btnDDL.textList = textList;
    btnDDL.fontSize = fontSizeF;
    
    return btnDDL;
}


#pragma mark - actions

- (void)buttonClick:(id)sender {
    if (self.textList.count == 0) {
        return;
    }
   
    [self setList];
}

- (void)buttonListClick:(UIButton *)sender {
    NSInteger currentIndex = sender.tag;
    if (currentIndex == self.selectedIndex) {
        [self.bgBtnView removeFromSuperview];
        [self.vCover removeFromSuperview];
        return;
    }
    self.selectedIndex = currentIndex;
    self.lblTitle.text = sender.titleLabel.text;
    
    // remove
    [self removeList];
    
#pragma mark back call
    // callBack
    if (self.drapDownListCallbackBlock) {
        self.drapDownListCallbackBlock(currentIndex);
    }
}

///vCover tapClick
- (void)vCoverAction:(UIControl *)sender {
    [self removeList];
}

#pragma mark - methods

- (void)removeList {
    // remove
    for (int i = 0; i < self.buttons.count; i++) {
        [self.buttons[i] removeFromSuperview];
    }
    [self.bgBtnView removeFromSuperview];
    [self.vCover removeFromSuperview];
}


#pragma mark - setupUI
- (void)setupUI {
    
    UILabel * lblTitle = [[UILabel alloc] init];
    [self insertSubview:lblTitle atIndex:0];
    
    [lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.leading.equalTo(self).offset(10);
    }];
    
    self.lblTitle = lblTitle;
    
    [self setNeedsDisplay];
}

- (void)setList {
    
    // create coverView
    UIControl * vCover = [[UIControl alloc] initWithFrame:self.window.bounds];
    [self.window addSubview:vCover];
    vCover.backgroundColor = [UIColor clearColor];
    self.vCover = vCover;
    [vCover addTarget:self action:@selector(vCoverAction:) forControlEvents:UIControlEventTouchDown];
    
    // create typeList
    UIView * vList = [[UIView alloc] init];
    self.bgBtnView = vList;
    [vCover addSubview:vList];
    vList.backgroundColor = [UIColor colorWithRed:232.0/255 green:232.0/255 blue:232.0/255 alpha:1];
    
    [vList gx_shadowWithColor:[UIColor blackColor] shadowRadius:5 shadowOpacity:1.0];
    
    NSMutableArray<UIButton *> * mArr = [NSMutableArray<UIButton *> array];
    for (int i=0; i < self.textList.count; i++) {
        UIButton * btn = [[UIButton alloc] init];
        [vList addSubview:btn];
        
        [btn setTitle:self.textList[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:self.fontSize];
        
        btn.tag = i;
        btn.backgroundColor = [UIColor colorWithRed:300/255.0 green:300/255.0 blue:300/255.0 alpha:1];
        
        [btn addTarget:self action:@selector(buttonListClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [mArr addObject:btn];
    }
    self.buttons = mArr.copy;
    
    CGFloat margin = 3;
    CGFloat bHeight = 30;
    CGFloat viewHeight = bHeight * self.buttons.count + margin * (self.buttons.count - 1);
    
    [vList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_bottom).offset(5);
        make.leading.equalTo(self).offset(10);
        make.trailing.equalTo(self).offset(-10);
        make.height.mas_offset(viewHeight);
    }];
    
    
    if (self.buttons.count == 1) {
        [self.buttons[0] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(vList);
        }];
    }else if (self.buttons.count == 2) {
        //
        [self.buttons[0] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.trailing.equalTo(vList);
            make.height.mas_offset(bHeight);
        }];
        
        [self.buttons[1] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.buttons[0].mas_bottom).mas_offset(margin);
            make.leading.trailing.equalTo(vList);
            make.height.mas_offset(bHeight);
        }];
    }else if (self.buttons.count > 2) {
        //
        [self.buttons[0] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.trailing.equalTo(vList);
            make.height.mas_offset(bHeight);
        }];
        
        for (int i = 1; i < self.buttons.count - 1; i++) {
            [self.buttons[i] mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.buttons[i-1].mas_bottom).mas_offset(margin);
                make.leading.trailing.equalTo(vList);
                make.height.mas_offset(bHeight);
            }];
        }
        
        [self.buttons[self.buttons.count - 1] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.buttons[self.buttons.count - 2].mas_bottom).mas_offset(margin);
            make.leading.trailing.equalTo(vList);
            make.height.offset(bHeight);
        }];
    }
}


#pragma mark - drawRect
- (void)drawRect:(CGRect)rect {
    //
    UIBezierPath * path = [UIBezierPath bezierPath];
    
    CGFloat xLength = self.bounds.size.height * 1.f/4;
    CGFloat yLength = 7.0/9 * self.bounds.size.height; //2 * (self.bounds.size.height * 1.f/3) + 1.0/3 * (self.bounds.size.height * 1.f/3);
    
    [path moveToPoint:CGPointMake(xLength, yLength)];
    [path addLineToPoint:CGPointMake(self.bounds.size.width - xLength, yLength)];
    
    [[UIColor grayColor] set];
    path.lineWidth = 2;
    
    [path stroke];
    
    [path addLineToPoint:CGPointMake(self.bounds.size.width - xLength, 2 * xLength)];
    [path addLineToPoint:CGPointMake(self.bounds.size.width - 2 * xLength, yLength)];
    
    [path fill];
}

#pragma mark - setter & getter
- (void)setText:(NSString *)text {
    _text = text.copy;
    
    self.lblTitle.text = text;
}

- (NSString *)text {
    return self.lblTitle.text;
}

//
- (void)setSelectedIndex:(NSInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    
    if (selectedIndex < self.textList.count) {
        self.lblTitle.text = self.textList[selectedIndex];
    }
}

//
- (void)setFontSize:(CGFloat)fontSize {
    _fontSize = fontSize;
    self.lblTitle.font = [UIFont systemFontOfSize:fontSize];
}

@end
